{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "乐学偶得版权所有  公众号：乐学Fintech  主讲人：William LX_LAB教研团队提供技术支持 正版视频： 网易云课堂：乐学偶得"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【综合题目】：写一个加密小程序，能够实现shift的加密，也能够实现shift解密"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "tips: abcdefg......z ord() chr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ord(\"a\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ord(\"b\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ord(\"c\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ord(\"d\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chr(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def encrypt():\n",
    "    plainText = input(\"你好，我是乐学偶得加解密小程序，请输入你想加密的文字：\")\n",
    "    shift = int(input(\"你好，请输入你想平移的个数：\"))\n",
    "    cipherText = \"\"\n",
    "    for ch in plainText:\n",
    "        if ch.isalpha():\n",
    "            stayInAlphabet = ord(ch) + shift\n",
    "        if stayInAlphabet > ord('z'):\n",
    "            stayInAlphabet -= 26\n",
    "        finalLetter = chr(stayInAlphabet)\n",
    "        cipherText += finalLetter\n",
    "    print (\"您好，您的加密已经完成，密文为：\", cipherText,\".我们是通过shift\",shift,\"位进行加密的。\")\n",
    "    \n",
    "encrypt()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def decrypte():\n",
    "    encryption=input(\"你好，我是乐学偶得加解密小程序，请输入你想解密的文字：\")\n",
    "    encryption_shift=int(input(\"你好，请输入你想回移的个数：\"))\n",
    "    \n",
    "    another_cipherText = \"\"\n",
    "    for c in encryption:\n",
    "        if c.isalpha():\n",
    "            another_stayInAlphabet = ord(c) - encryption_shift\n",
    "        if another_stayInAlphabet >ord(\"z\"):\n",
    "            another_stayInAlphabet += 26\n",
    "        another_finalLetter = chr(another_stayInAlphabet)\n",
    "        another_cipherText += another_finalLetter\n",
    "    print (\"您好，您的解密已经完成，明文为：\", another_cipherText,\".我们是通过shift\",encryption_shift,\"位进行解密的。\")\n",
    "    \n",
    "decrypte()     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tkinter import *\n",
    "\n",
    "menu=Tk()\n",
    "menu.title(\"乐学偶得的加解密小程序\")\n",
    "menu.geometry(\"300x200\")\n",
    "button1 = Button(menu,text=\"按这个按钮进行加密\",command=encrypt)\n",
    "button1.pack()\n",
    "\n",
    "button2 = Button(menu,text=\"按这个按钮进行解密\",command=decrypte)\n",
    "button2.pack()\n",
    "\n",
    "button3 = Button(menu,text=\"按这个按钮退出\",command=exit)\n",
    "button3.pack()\n",
    "\n",
    "menu.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
